var dvs = dvs || {};

dvs.getContextPath = function() {
    return dvs.contextPath;
};

dvs.ajax = function(settings) {
    var url = settings.url;
    if (!url) {
        dvs.alert("Illegal url：" + url);
        return false;
    }
    if (url.charAt(0) != "/") {
        url = "/" + url;
    }
    url = "http://" +window.location.host + url;

    var type = settings.type;
    if (!type) {
        type = "POST";
    }

    var async = settings.async;
    if (async == undefined || async == null) {
        async = true;
    }

    var parseJSON = settings.parseJSON;
    if (parseJSON == undefined || parseJSON == null) {
        parseJSON = true;
    }

    var reqMap = {};
    if (settings.form) {
        var formMap = dvs.collectForm(settings.form);
        reqMap = dvs.merge(reqMap, formMap);
    }
    if (settings.param) {
        reqMap = dvs.merge(reqMap, settings.param);
    }

    var data = reqMap;

    var timeout = settings.timeout;
    if (!timeout) {
        timeout = 15000;
    }

    var loading = settings.loading;
    if (!loading) {
        loading = "none";
    }

    var $loading = null;
    if (loading == "none") {
        // $loading = dvs.showLoading();
    } else if (loading == "modal") {
        // $loading = dvs.showLoading();
    } else if (loading == "notice") {
        // $loading = dvs.showLoading();
    } else if (loading == "text") {
        var showText = settings.showText;
        if (!showText) {
            loading = "加载中";
        }
        // $loading = dvs.showLoadingText(showText);
    } else {
        // $loading = dvs.showLoading(loading);
    }

    var ajaxSettings = {
        url : url,
        type : type,
        async : async,
        data : data,
        dataType : "text",
        timeout : timeout
    };
    $.ajax(ajaxSettings).always(function() { // data|jqXHR, textStatus, jqXHR|errorThrown
        if ($loading) {
            dvs.removeLoading($loading);
        }
    }).done(function(respData, textStatus, jqXHR) {
        if (settings.callback) {
            if (respData && parseJSON) {
                try {
                    respData = dvs.parseJSON(respData);
                } catch (e) {
                }
            }

            var t = $.type(settings.callback);
            if (t == "function") {
                settings.callback(respData);
            } else if (t == "array") {
                for (var i = 0; i < settings.callback.length; i++) {
                    var callback_i = settings.callback[i];
                    if ($.type(callback_i) == "function") {
                        callback_i(respData);
                    }
                }
            }
        }
    }).fail(function(jqXHR, textStatus, errorThrown) {
        if (settings.fail) {
            var t = $.type(settings.fail);
            if (t == "function") {
                settings.fail(jqXHR, textStatus, errorThrown);
            } else if (t == "array") {
                for (var i = 0; i < settings.fail.length; i++) {
                    var fail_i = settings.fail[i];
                    if ($.type(fail_i) == "function") {
                        fail_i(jqXHR, textStatus, errorThrown);
                    }
                }
            }
        }
    });
};

dvs.parseJSON = function(json) {
    if (json) {
        return $.parseJSON(json);
    } else {
        return {};
    }
};

dvs.merge = function() {
    var ret = {};
    var args = arguments;
    for (var i = 0; i < args.length; i++) {
        var arg = args[i];
        if (arg) {
            var type = $.type(arg);
            if (type == "object") {
                for ( var key in arg) {
                    var value = arg[key];
                    if (ret.hasOwnProperty(key)) {
                        var v = ret[key];
                        if ($.type(v) == "array") {
                            v.push(value);
                        } else {
                            var array = [];
                            array.push(v);
                            array.push(value);
                            ret[key] = array;
                        }
                    } else {
                        ret[key] = value;
                    }
                }
            } else {
                dvs.alert("Illegal arg：" + arg + ", type=" + type);
                return false;
            }
        }
    }
    return ret;
};

dvs.collectForm = function(form) {
    var type = $.type(form);
    var forms = [];
    if (type == "string") {
        forms.push(form);
    } else if (type == "array") {
        forms = form;
    } else {
        dvs.alert("Illegal form：" + form + ", type=" + type);
        return false;
    }

    var ret = {};
    $.each(forms, function(index, fromId) {
        var $form = $("#" + fromId);
        $("input,select,textarea", $form).each(function(index, ele) {
            var $ele = $(ele);
            var type = $ele.attr("type");
            var valid = true;
            if (type == "checkbox" || type == "radio") {
                valid = $ele.is(":checked");
            }
            if (valid) {
                var key = $ele.attr("name");
                var value = $ele.val();
                if (key) {
                    if (ret.hasOwnProperty(key)) {
                        var v = ret[key];
                        if ($.type(v) == "array") {
                            v.push(value);
                        } else {
                            var array = [];
                            array.push(v);
                            array.push(value);
                            ret[key] = array;
                        }
                    } else {
                        ret[key] = value;
                    }
                }
            }
        });
    });
    return ret;
};

dvs.alert = function(msg) {
    alert(msg);
};